//
//  NotifyView.m
//  location
//
//  Created by Dongxiang Xu on 2025/9/29.
//

#import "NotifyView.h"
#import "XDXLocationManager.h"

@interface NotifyView ()



@end

@implementation NotifyView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)initView
{
    UIView *infoView = [[UIView alloc] initWithFrame:CGRectMake(12, SCREEN_HEIGHT, SCREEN_WIDTH-24, 554)];
    infoView.layer.masksToBounds = YES;
    [infoView.layer setCornerRadius:40];
    infoView.backgroundColor = RealWhiteColor;
    [self addSubview:infoView];
    
    UIButton *close = [[UIButton alloc] initWithFrame:CGRectMake(infoView.width-46, 16, 30, 30)];
    [close setImage:[UIImage imageNamed:@"map_close"] forState:UIControlStateNormal];
    [infoView addSubview:close];
    [[close rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [UIView animateWithDuration:0.3 animations:^{
            infoView.y = SCREEN_HEIGHT;
            self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0];
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }];
    
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake((infoView.width-240)/2, 78, 240, 240)];
    img.image = [UIImage imageNamed:@"notify_view"];
    [infoView addSubview:img];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(20, img.maxY+28, infoView.width-40, 28)];
    title.font = MEDIUM_FONT(20);
    title.textColor = RealBlackColor;
    title.textAlignment = NSTextAlignmentCenter;
    [infoView addSubview:title];
    if ([[XDXLocationManager shareInstance].locationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedAlways) {
        title.text = @"始终允许定位";
    }else if ([[XDXLocationManager shareInstance].locationManager accuracyAuthorization]==CLAccuracyAuthorizationReducedAccuracy){
        title.text = @"打开精确定位";
    }
    
    UILabel *subTitle = [[UILabel alloc] initWithFrame:CGRectMake(20, title.maxY+4, infoView.width-40, 28)];
    subTitle.font = DEFAULT_FONT(15);
    subTitle.textColor = GraryTooHeavyColor;
    subTitle.textAlignment = NSTextAlignmentCenter;
    [infoView addSubview:subTitle];
    if ([[XDXLocationManager shareInstance].locationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedAlways) {
        subTitle.text = @"与家人分享你的位置，需要将位置访问权限设置为“始终”，并打开“精确定位”";
        subTitle.numberOfLines = 2;
        [subTitle sizeToFit];
        [subTitle setFrame:CGRectMake((infoView.width-subTitle.width)/2, title.maxY+4, subTitle.width, subTitle.height)];
    }else if ([[XDXLocationManager shareInstance].locationManager accuracyAuthorization]==CLAccuracyAuthorizationReducedAccuracy){
        subTitle.text = @"与家人分享你的位置，需要打开“精确定位”";
    }
    
    UIButton *setting = [[UIButton alloc] initWithFrame:CGRectMake(20, subTitle.maxY+28, infoView.width-40, 54)];
    setting.backgroundColor = RedColor;
    setting.layer.masksToBounds = YES;
    [setting.layer setCornerRadius:27];
    [setting.titleLabel setFont:MEDIUM_FONT(16)];
    [setting setTitleColor:RealWhiteColor forState:UIControlStateNormal];
    [setting setTitle:@"前往设置" forState:UIControlStateNormal];
    [infoView addSubview:setting];
    [[setting rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [UIView animateWithDuration:0.3 animations:^{
            infoView.y = SCREEN_HEIGHT;
            self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0];
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
        NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionUniversalLinksOnly:@NO} completionHandler:nil];
    }];
    
    UIButton *wait = [[UIButton alloc] initWithFrame:CGRectMake(20, setting.maxY+16, infoView.width-40, 22)];
    [wait.titleLabel setFont:DEFAULT_FONT(16)];
    [wait setTitleColor:GraryHeavyLightColor forState:UIControlStateNormal];
    [wait setTitle:@"稍后" forState:UIControlStateNormal];
    [infoView addSubview:wait];
    [[wait rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [UIView animateWithDuration:0.3 animations:^{
            infoView.y = SCREEN_HEIGHT;
            self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0];
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }];
    
    infoView.height = wait.maxY+30;
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self insertSubview:bgView atIndex:0];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [[tap rac_gestureSignal] subscribeNext:^(id x) {
        [UIView animateWithDuration:0.3 animations:^{
            infoView.y = SCREEN_HEIGHT;
            self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0];
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }];
    [bgView addGestureRecognizer:tap];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0.4];
        infoView.y = SCREEN_HEIGHT-infoView.height-32;
    }];
}

@end
